/** ***********************************************************************
*  $Id: $
*
*  COPYRIGHT @2008 CSS CORP PVT LTD
*          
*  This program is free software; you can redistribute it and/or modify it under the terms of 
*  version 2 of the GNU General Public License as published by the Free Software 
*  Foundation.
*
*  The above copyright notice and this permission notice shall be included in all copies 
*  or substantial portions of the Software.
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
*  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
*  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY
*  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
*  DEALINGS IN THE SOFTWARE.
*
************************************************************************* */

package com.css.ctg.tulip.util;

import org.apache.log4j.Logger;


/**
 * TulipLogger is an logger utility which implements the Log4j classes.
 * 
 * @version $Rev:
 * 			$  
 * 
 **/
public class TulipLogger {
	
	private Logger logger;
	
	/**
	 * This attribute would be passed to the log method to specify the logType as INFO.
	 */
	public static final String info = "info";
	/**
	 * This attribute would be passed to the log method to specify the logType as DEBUG.
	 */
	public static final String debug = "debug";
	/**
	 * This attribute would be passed to the log method to specify the logType as WARN.
	 */
	public static final String warn = "warn";
	/**
	 * This attribute would be passed to the log method to specify the logType as ERROR.
	 */
	public static final String error = "error";
	
	
	/**
	 * This is a protected method to initialize the Log4j logger object
	 * 
	 * @param className
	 * @return void
	 */
	protected void initializeLogger(String className) {
		logger = Logger.getLogger(className);
	}
	
	/**
	 * This overloaded method logs to the log file based on the logType
	 * 
	 * @param className
	 * @param logType
	 * @param logMessage
	 * @param t
	 * @return void
	 */
	
	public static void log(String className, String logType, String logMessage, Throwable t) {
		
		TulipLogger tulipLogger = new TulipLogger();
		tulipLogger.initializeLogger(className);
		
		if(logType != null && TulipLogger.info.equals(logType)) {
			tulipLogger.info(logMessage, t);
		} 
		
		if(logType != null && TulipLogger.debug.equals(logType)) {
			tulipLogger.debug(logMessage, t);
		}
		
		if(logType != null && TulipLogger.warn.equals(logType)) {
			tulipLogger.warn(logMessage, t);
		}
		
		if(logType != null && TulipLogger.error.equals(logType)) {
			tulipLogger.error(logMessage, t);
		}
	}
	
	/**
	 * This overloaded method logs to the log file based on the logType
	 * 
	 * 
	 * @param className
	 * @param logType
	 * @param logMessage
	 * @return void
	 */
	
	public static void log(String className, String logType, String logMessage) {
		
		TulipLogger.log(className, logType, logMessage, null);		
	}
	
	/**
	 * This method would be called by the log method if the logType == INFO
	 * 
	 * @param logMessage
	 * @param t
	 * @return void
	 */
	
	private void info(String logMessage, Throwable t) {
		if(t == null) {
			logger.info(logMessage);
		} else {
			logger.info(logMessage, t);
		}		
	}
	
	/**
	 * This method would be called by the log method if the logType == DEBUG
	 * 
	 * @param logMessage
	 * @param t
	 * @return void
	 */
	
	private void debug(String logMessage, Throwable t) {
		if(t == null) {
			logger.debug(logMessage);
		} else {
			logger.debug(logMessage, t);
		}
	}
	
	/**
	 * This method would be called by the log method if the logType == WARN
	 * 
	 * @param logMessage
	 * @param t
	 * @return void
	 */
	private void warn(String logMessage, Throwable t) {
		if(t == null) {
			logger.warn(logMessage);
		} else {
			logger.warn(logMessage, t);
		}
	}
	
	/**
	 * This method would be called by the log method if the logType == ERROR
	 * 
	 * @param logMessage
	 * @param t
	 * @return void
	 */
	private void error(String logMessage, Throwable t) {
		if(t == null) {
			logger.error(logMessage);
		} else {
			logger.error(logMessage, t);
		}
	}
}
